{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "7a6ed531",
      "metadata": {
        "id": "7a6ed531"
      },
      "source": [
        "# AgentWorkflow & FunctionAgent Beginner Guide\n",
        "\n",
        "This notebook walks you through setting up and using a basic `AgentWorkflow` with a single `FunctionAgent` using the `llama-index` framework."
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/DhivyaBharathy-web/PraisonAI/blob/main/examples/cookbooks/FunctionAgent_Workflow.ipynb)"
      ],
      "metadata": {
        "id": "vR_DwtA9kwNX"
      },
      "id": "vR_DwtA9kwNX"
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Dependencies"
      ],
      "metadata": {
        "id": "xrCnRE5uhLZB"
      },
      "id": "xrCnRE5uhLZB"
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "05277ffb",
      "metadata": {
        "id": "05277ffb"
      },
      "outputs": [],
      "source": [
        "%pip install llama-index tavily-python"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "49f41051",
      "metadata": {
        "id": "49f41051"
      },
      "source": [
        "## Setup OpenAI LLM"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "id": "456794b8",
      "metadata": {
        "id": "456794b8"
      },
      "outputs": [],
      "source": [
        "from llama_index.llms.openai import OpenAI\n",
        "\n",
        "llm = OpenAI(model=\"gpt-5-nano\", api_key=\"Enter your api key here\")  # Replace with your OpenAI API key\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "4923571f",
      "metadata": {
        "id": "4923571f"
      },
      "source": [
        "## Define Web Search Tool"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "id": "6e5f265a",
      "metadata": {
        "id": "6e5f265a"
      },
      "outputs": [],
      "source": [
        "from tavily import AsyncTavilyClient\n",
        "\n",
        "async def search_web(query: str) -> str:\n",
        "    \"\"\"Useful for using the web to answer questions.\"\"\"\n",
        "    client = AsyncTavilyClient(api_key=\"Enter your api key here\")  # Replace with your Tavily API key\n",
        "    return str(await client.search(query))\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "a4c5f890",
      "metadata": {
        "id": "a4c5f890"
      },
      "source": [
        "## Create FunctionAgent"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 14,
      "id": "d5f552ec",
      "metadata": {
        "id": "d5f552ec"
      },
      "outputs": [],
      "source": [
        "from llama_index.core.agent.workflow import FunctionAgent\n",
        "\n",
        "agent = FunctionAgent(\n",
        "    tools=[search_web],\n",
        "    llm=llm,\n",
        "    system_prompt=\"You are a helpful assistant that can search the web for information.\",\n",
        ")\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "5d7b4245",
      "metadata": {
        "id": "5d7b4245"
      },
      "source": [
        "## Run the Agent"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "id": "49b31603",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "49b31603",
        "outputId": "a729c081-a9a0-4019-8b79-5bfdc395f9ce"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "The current weather in San Francisco is as follows:\n",
            "\n",
            "- **Temperature**: 13.3°C (55.9°F)\n",
            "- **Condition**: Mist\n",
            "- **Wind**: 8.3 mph (13.3 kph) from the WSW\n",
            "- **Humidity**: 90%\n",
            "- **Visibility**: 16 km (9 miles)\n",
            "- **Feels Like**: 12.2°C (53.9°F)\n",
            "\n",
            "For more details, you can check the [Weather API](https://www.weatherapi.com/).\n"
          ]
        }
      ],
      "source": [
        "response = await agent.run(user_msg=\"What is the weather in San Francisco?\")\n",
        "print(str(response))\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "93c85265",
      "metadata": {
        "id": "93c85265"
      },
      "source": [
        "## Use AgentWorkflow"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 16,
      "id": "a303658b",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "a303658b",
        "outputId": "fcd07905-5300-4cb3-80c1-fb2bf0addc20"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "The current weather in San Francisco is as follows:\n",
            "\n",
            "- **Temperature**: 13.3°C (55.9°F)\n",
            "- **Condition**: Mist\n",
            "- **Wind**: 8.3 mph (13.3 kph) from the WSW\n",
            "- **Humidity**: 90%\n",
            "- **Visibility**: 16 km (9 miles)\n",
            "- **Feels Like**: 12.2°C (53.9°F)\n",
            "\n",
            "For more details, you can check the [Weather API](https://www.weatherapi.com/).\n"
          ]
        }
      ],
      "source": [
        "from llama_index.core.agent.workflow import AgentWorkflow\n",
        "\n",
        "workflow = AgentWorkflow(agents=[agent])\n",
        "response = await workflow.run(user_msg=\"What is the weather in San Francisco?\")\n",
        "print(str(response))\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "a9e1ed26",
      "metadata": {
        "id": "a9e1ed26"
      },
      "source": [
        "## Maintain Context State"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 17,
      "id": "c1ba228f",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "c1ba228f",
        "outputId": "e7787f4a-f1fb-438c-bb11-f1ba243c3455"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Nice to meet you, Logan! How can I assist you today?\n",
            "Your name is Logan.\n"
          ]
        }
      ],
      "source": [
        "from llama_index.core.workflow import Context\n",
        "\n",
        "ctx = Context(agent)\n",
        "response = await agent.run(user_msg=\"My name is Logan, nice to meet you!\", ctx=ctx)\n",
        "print(str(response))\n",
        "\n",
        "response = await agent.run(user_msg=\"What is my name?\", ctx=ctx)\n",
        "print(str(response))\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "97ec9b2f",
      "metadata": {
        "id": "97ec9b2f"
      },
      "source": [
        "## Serialize Context"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 18,
      "id": "21aa311f",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "21aa311f",
        "outputId": "f8c231d6-19d0-415d-e27f-c67ad2b7f684"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Yes, I remember your name is Logan.\n"
          ]
        }
      ],
      "source": [
        "from llama_index.core.workflow import JsonSerializer\n",
        "\n",
        "ctx_dict = ctx.to_dict(serializer=JsonSerializer())\n",
        "restored_ctx = Context.from_dict(agent, ctx_dict, serializer=JsonSerializer())\n",
        "\n",
        "response = await agent.run(user_msg=\"Do you still remember my name?\", ctx=restored_ctx)\n",
        "print(str(response))\n"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "provenance": []
    },
    "language_info": {
      "name": "python"
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
